<script>
//var items=<?php echo json_encode($items); ?>;
function editach(data)
{
$('#achShowguiachForm').find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

$('#achNew').val('true');
if (data!="")
{
	if (data[0]==1)
		$('#achActive').attr('checked', true);
	else
		$('#achActive').attr('checked', false);
	$('#achNew').val('false');
	
	$('#achName').val(data[1]);
	$('#achMaxarwards').val(data[2]);
	$('#achCategory').val(data[3]);
	changecat();
	$('#achKey').val(data[4]);
	$('#achValue').val(data[5]);
	$('#achDescription').val(data[6]);
	
	a = data[7].split(";");
	for(var i= 0; i < a.length; i++)
	{
		b = a[i].split(' ');
		switch(b[0])
		{
			case 'item':
				$('#achItem').val(b[1]);
				$('#achAmount').val(b[2]);
				break;
			case 'money':
				$('#achMoney').val(b[1]);
				break;
			case 'group':
				$('#achGroup').val(b[1]);
				break;
		}
	}
	$('#achListach').val(data[8]);
}
$.blockUI({
	theme: true,
	css:{ 
		padding:        0, 
		margin:         0, 
		width:          '300px'
		},
	title:    'Edit achievement',
	fadeIn:  300,
	message: $('#editach')});
}

function changecat()
{
	$('#achKey option').remove();
	$('#achKey').append($("<option></option>").attr("value","").text("")); 

	if($('#achCategory').val()=="stats")
	{
		$('#achKey').append($("<option></option>").attr("value","armswing").text("armswing")); 
		$('#achKey').append($("<option></option>").attr("value","ban").text("ban")); 
		$('#achKey').append($("<option></option>").attr("value","chat").text("chat")); 
		$('#achKey').append($("<option></option>").attr("value","chatletters").text("chatletters")); 
		$('#achKey').append($("<option></option>").attr("value","command").text("command")); 
		$('#achKey').append($("<option></option>").attr("value","damagehealed").text("damagehealed")); 
		$('#achKey').append($("<option></option>").attr("value","ipban").text("ipban")); 
		$('#achKey').append($("<option></option>").attr("value","kick").text("kick")); 
		$('#achKey').append($("<option></option>").attr("value","login").text("login")); 
		$('#achKey').append($("<option></option>").attr("value","move").text("move")); 
		$('#achKey').append($("<option></option>").attr("value","playedfor").text("playedfor")); 
		$('#achKey').append($("<option></option>").attr("value","teleport").text("teleport")); 
	};
	if(($('#achCategory').val()=="kills") || ($('#achCategory').val()=="deaths") || ($('#achCategory').val()=="damagetaken") || ($('#achCategory').val()=="damagedealt"))
	{
		$('#achKey').append($("<option></option>").attr("value","armswing").text("armswing")); 
		$('#achKey').append($("<option></option>").attr("value","chicken").text("chicken")); 
		$('#achKey').append($("<option></option>").attr("value","cow").text("cow")); 
		$('#achKey').append($("<option></option>").attr("value","creeper").text("creeper")); 
		$('#achKey').append($("<option></option>").attr("value","creeperexplosion").text("creeperexplosion")); 
		$('#achKey').append($("<option></option>").attr("value","DROWINING").text("DROWINING")); 
		$('#achKey').append($("<option></option>").attr("value","FALL").text("FALL")); 
		$('#achKey').append($("<option></option>").attr("value","fire").text("fire")); 
		$('#achKey').append($("<option></option>").attr("value","Ghast").text("Ghast")); 
		$('#achKey').append($("<option></option>").attr("value","LAVA").text("LAVA")); 
		$('#achKey').append($("<option></option>").attr("value","pig").text("pig")); 
		$('#achKey').append($("<option></option>").attr("value","PigZombie").text("PigZombie")); 
		$('#achKey').append($("<option></option>").attr("value","player").text("player")); 
		$('#achKey').append($("<option></option>").attr("value","sheep").text("sheep")); 
		$('#achKey').append($("<option></option>").attr("value","skeleton").text("skeleton")); 
		$('#achKey').append($("<option></option>").attr("value","spider").text("spider")); 
		$('#achKey').append($("<option></option>").attr("value","spiderjockey").text("spiderjockey")); 
		$('#achKey').append($("<option></option>").attr("value","squid").text("squid")); 
		$('#achKey').append($("<option></option>").attr("value","total").text("total")); 
		$('#achKey').append($("<option></option>").attr("value","zombie").text("zombie")); 
	}
	if(($('#achCategory').val()=="blockdestroy") || ($('#achCategory').val()=="blockcreate"))
	{
	<?php 
	foreach($blocks as $block)
	{
		echo '$(\'#achKey\').append($("<option></option>").attr("value","'.$block.'").text("'.$block.'"));';
	}
	?>
	}
	if(($('#achCategory').val()=="itemuse") || ($('#achCategory').val()=="itempickup") || ($('#achCategory').val()=="itemdrop"))
	{
	<?php 
	foreach($items as $item)
	{
		echo '$(\'#achKey\').append($("<option></option>").attr("value","'.$item.'").text("'.$item.'"));';
	}
	?>
	}	
}
</script>
<div>
<table class="box-table-a">
<thead>
<tr>
<th scope='col'><?php echo __('Enabled',true); ?></th>
<th scope='col'><?php echo __('Name',true); ?></th>
<th scope='col'><?php echo __('Max awards',true); ?></th>
<th scope='col'><?php echo __('Category',true); ?></th>
<th scope='col'><?php echo __('Key',true); ?></th>
<th scope='col'><?php echo __('Value',true); ?></th>
<th scope='col'><?php echo __('Description',true); ?></th>
<th scope='col'><?php echo __('Commands',true); ?></th>
<th scope='col'><?php echo __('Conditions',true); ?></th>
<th scope='col'></th>
<th scope='col'></th>
</tr>
</thead>
<tbody id="plugins_showach">
<?php echo $this->element('plugins_showach'); ?>
</tbody>
<tfoot>
<tr><td colspan="11" align="center"><a class="ui-icon ui-icon-plus" href="#" onclick="editach('');"></a></td></tr>
</tfoot>
</table>	
</div>
<center>
<?php
echo __('Nb of Achievements : ',true).$count."<br/>";
for($index=0;$index<($count/10);$index++)
{
	if ($index==$show)
		echo "<b><font size=+1>".$index." </font></b>";
	else
		echo $js->link($index,array('action'=>showguiach,$index),array('update'=>'#panelconfig')).' ';
};
echo $this->Js->writeBuffer();
?>
</center>

<div id="editach" class="dialog" style="display:none">
<?php
echo $form->create('ach');
echo $form->input('new',array('type'=>'hidden'));
echo $form->label('active',__('Active',true));
echo $form->checkbox('active',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->input('name',array('div'=>false));
echo "<br/>";
echo $form->input('maxarwards',array('label'=>'Max awards','div'=>false));
echo "<br/>";
echo $form->label('category',__('Category',true));
echo $form->select('category',$category,NULL,array('div'=>false,'onchange'=>'changecat();'));
echo "<br/>";
//echo $form->input('group',array('div'=>false));
echo $form->label('key',__('Key or item',true));
echo $form->select('key',NULL,NULL,array('div'=>false));
echo "<br/>";
echo $form->input('value',array('div'=>false));
echo "<br/>";
echo $form->input('description',array('div'=>false));
echo "<br/><fieldset>";
echo "<legend>".__('Reward items',true)."</legend>";
echo $form->label('item',__('Give item',true));
echo $form->select('item',array_merge($blocks,$items),NULL,array('div'=>false));
echo "<br/>";
echo $form->input('amount',array('div'=>false));
echo "</fieldset>";
echo "<fieldset>";
echo "<legend>".__('Add group',true)."</legend>";
echo $form->input('group',array('div'=>false));
echo "</fieldset>";
echo "<fieldset>";
echo "<legend>".__('Reward money',true)."</legend>";
echo $form->input('money',array('div'=>false));
echo "</fieldset>";
echo "<fieldset>";
echo "<legend>".__('Conditions',true)."</legend>";
echo $form->label('listach',__('Name',true));
echo $form->select('listach',$listach,NULL,array('div'=>false,'escape'=>false));
echo "</fieldset>";
?>
<center>
<?php
echo $js->submit(
'save',
array('div'=>false,'url'=>array('controller'=>'plugins','action'=>'editach'),'update'=>'#plugins_showach','complete' => 'saveUI();'));
?>
<input type="reset" value="cancel" onclick="$.unblockUI();">
</center>
</div>
<?php
echo $this->Js->writeBuffer();
?>